package com.jxc.action.kcgl;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxc.action.BaseAction;
import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Deliverlist;
import com.jxc.model.po.Transferlist;
import com.jxc.model.po.TransferlistItem;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.VDeliverlist;
import com.jxc.model.vo.VDeliverlistItem;
import com.jxc.service.DeliverlistItemServiceI;
import com.jxc.service.DeliverlistServiceI;
import com.jxc.service.TransferlistItemServiceI;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 库存调拨明细
 * 
 * @author Tino
 * 
 */
@Namespace("/kcgl")
@Action(value = "goodsDeliverItemAction", results = { @Result(name = "goodsDeliverItemAdd", location = "/amountRelate/goodsDeliverItemAdd.jsp"),
		@Result(name = "goodsDeliverItemEdit", location = "/amountRelate/goodsDeliverItemEdit.jsp"),
		@Result(name = "goodsReceiveEdit", location = "/amountRelate/goodsReceiveEdit.jsp"),
		@Result(name = "goodsReceiveMatch", location = "/amountRelate/goodsReceiveMatch.jsp")})
public class GoodsDeliverItemAction extends BaseAction implements ModelDriven<VDeliverlistItem> {
	private final static Logger logger = Logger.getLogger(GoodsDeliverItemAction.class);

	private VDeliverlistItem model = new VDeliverlistItem();
	@Autowired
	private DeliverlistServiceI deliverlistService;
	@Autowired
	private DeliverlistItemServiceI deliverlistItemService;
	@Autowired
	private BaseDaoI<Deliverlist> Itemdao;

	/**
	 * 向前端发送当前deliverlist对应的VDeliverlistItem对象格式为datagrid
	 */
	public void datagrid() {
		DataGrid dg = deliverlistItemService.datagrid(model);
		writeJson(dg);
	}

	public String goodsDeliverItemAdd() {
		return "goodsDeliverItemAdd";
	}

	public String goodsDeliverItemEdit() {
		try {
			this.getRequest().setAttribute("id", model.getId());
			Deliverlist dl = Itemdao.get(Deliverlist.class,model.getId());
			this.getRequest().setAttribute("IsInConfirm", dl.getIsInConfirm());
			this.getRequest().setAttribute("IsOutConfirm",dl.getIsOutConfirm());
			this.getRequest().setAttribute("dealerName", dl.getDealer().getUsername());
			this.getRequest().setAttribute("orname", dl.getOutRepertory().getName());
			this.getRequest().setAttribute("num", dl.getNum());
			this.getRequest().setAttribute("date", dl.getDate());
			this.getRequest().setAttribute("serial", dl.getSerial());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "goodsDeliverItemEdit";
	}
	
	public String goodsReceiveEdit() {
		try {
			this.getRequest().setAttribute("id", model.getId());
			Deliverlist dl = Itemdao.get(Deliverlist.class,model.getId());			
			this.getRequest().setAttribute("IsInConfirm", dl.getIsInConfirm());
			this.getRequest().setAttribute("IsOutConfirm",dl.getIsOutConfirm());
			this.getRequest().setAttribute("agencyName", dl.getAgency().getUsername());
			this.getRequest().setAttribute("orname", dl.getOutRepertory().getName());
			this.getRequest().setAttribute("num", dl.getNum());
			this.getRequest().setAttribute("date", dl.getDate());
			this.getRequest().setAttribute("serial", dl.getSerial());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "goodsReceiveEdit";
	}
	
	public String noAuth_goodsReceiveMatch() {
		return "goodsReceiveMatch";
	}

	@Override
	public VDeliverlistItem getModel() {
		return model;
	}
	public void delete() {
		// TODO 判断要删除的调拔单中是否已经确认过，只有未确认过的调拔单才能删除
		Json j = new Json();
		deliverlistItemService.delete(model.getIds());
		j.setSuccess(true);
		j.setMsg("删除成功!");
		super.writeJson(j);
	}
}
